<%--
  Created by IntelliJ IDEA.
  User: XPS15
  Date: 2022/2/28
  Time: 10:58
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
  String path = request.getContextPath();
  String basePath = request.getScheme() + "://" + request.getServerName() + ":" +
          request.getServerPort() + path + "/";
%>
<base href="<%=basePath%>">
<html>
  <head>
    <title>注册</title>
    <script src="<%=basePath%>js/jquery-1.8.3.min.js"></script>
  </head>

  <body>
  <table style="border: 0;margin-left: 40%;margin-top: 200px">
    <tr>
      <td><label for="name">用户名:</label></td>
      <td><input type="text" name="name" id="name" required="required" value="" /></td>
    </tr>
    <tr>
      <td><label>手机号:</label></td>
      <td><input type="text" name="phone" id="phone" value="" /></td>
    </tr>
    <tr>
      <td><label for="password">密码:</label></td>
      <td><input type="password" name="password" required="required" id="password" value="" /></td>
    </tr>
    <tr>
      <td><label for="password2">确认密码:</label></td>
      <td><input type="password" name="password2" required="required" id="password2" value="" /></td>
    </tr>
    </tr>

  </table>
  <input style="margin-left: 45%;margin-top: 10px" type="button" value="提交" onclick="submit()"/>
  <tr>已有账号，<a href="<%=basePath%>view/login.jsp">去登录</a></tr>
  </body>
<script>
  function submit(){
    var name = $("#name").val();
    var phone = $("#phone").val();
    var pwd1 = $("#password").val();
    var pwd2 = $("#password2").val();
    //判断是否为空，且两个密码是否相同
    if(name == ""){
      alert("用户名不能为空");
      return;
    }
    if(phone == ""){
      alert("手机号不能为空");
      return;
    }
    if(pwd1 == ""){
      alert("密码不能为空");
      return;
    }
    if(pwd1 != pwd2){
      alert("密码不相同");
      return;
    }
    //先验证注册信息是否已经存在
    $.ajax({
      type:"POST",
      url:"/myweb/userServlet",
      data: {
        name:name,
        phone:phone,
        con:"val"
      },
      success:function (res){
        if(res == "phis"){
          alert("手机号已存在");
          return;
        }
        if(res == "nameis"){
          alert("用户名已存在");
          return;
        }
        //信息不重复可以注册
        if(res == "yes"){
          $.ajax({
            async: false,
            type:"POST",
            url:"/myweb/userServlet",
            data:{
              name:name,
              phone:phone,
              password:pwd1,
              con:"reg"
            },
            success:function (result) {
              if(result=='yes'){
                alert("注册成功")
              }
              if(result=='no'){
                alert("注册失败");
              }
            },
            error:function (err) {
              alert("系统错误,无法与后台进行数据交互");
            }
          });
        }
      }
    })
  }
</script>
</html>
